from flask_restplus import Resource, Namespace, fields, marshal_with
from common.utils.decorators import login_required
from flask import g, request, jsonify
from common.models import db
from app import redis_clent, app
from common.models.hello import Hello
from flask_restplus import reqparse, inputs
from app.routes.hello.schemas import hello_mds, post_hello_mds
from app.tasks.test import test


hello_ns = Namespace("hello")
# hello_ns.schema_model(name=Hello, schema="hello")

hello_md = hello_mds(hello_ns)
hello_md_post = post_hello_mds(hello_ns, hello_md)


class HelloWord(Resource):

    hello_md = hello_mds(hello_ns)
    hello_md_post = post_hello_mds(hello_ns, hello_md)
    # method_decorators = {"get": [login_required]} # 检查用户登录的装饰器
    @marshal_with(hello_md)  # 指定序列化返回
    @hello_ns.doc(params={"name": "a name"})  # 指定输入字段
    def get(self):
        data = request.args
        result = test.delay()

        return data

    @hello_ns.expect(hello_md, validate=True)
    @hello_ns.marshal_with(hello_md_post)
    def post(self):
        json = request.json
        # data = hello_ns.payload
        data = []
        print(json.get("name"))
        data.append({"name": json.get("name")})
        return {"total": json.get("total"), "data": data}
